package com.attendancesystem.controller;

import com.attendancesystem.util.JDBCUtils;
import com.fasterxml.jackson.databind.node.ObjectNode;
import jakarta.servlet.http.*;


import jakarta.servlet.ServletException;

import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.sql.*;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

import java.util.Arrays;
import java.util.Enumeration;

@WebServlet(name = "AddLectureController", value = "/AddLectureController")

public class AddLectureController extends HttpServlet {
    private static final ObjectMapper objectMapper = new ObjectMapper();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    doPost(request,response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        System.out.println("111");
        request.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
//        response.setContentType("text/html;charset=UTF-8");
        response.setContentType("application/json;charset=UTF-8"); // 增加字符集定义
        BufferedReader reader = request.getReader();
        JsonNode rootNode = objectMapper.readTree(reader);

        // 提取参数
        String courseId = rootNode.get("courseId").asText();
        String className = rootNode.get("className").asText();
        String lectureTime = rootNode.get("lectureTime").asText();
        String classroom = rootNode.get("classroom").asText();
        String summary = rootNode.get("summary").asText();
//        System.out.println("courseId:"+courseId);
//        System.out.println("className:"+className);
//
//        System.out.println("classroom:"+classroom);
//        System.out.println("summary:"+summary);



        String sql="INSERT INTO lectures (course_id,class_name,lecture_time,classroom,summary) " +
                "VALUES (?, ?, ?, ?, ?)";
        try( Connection con = JDBCUtils.getConnection(); // 必须从连接池获取
             PreparedStatement stm = con.prepareStatement(sql)){
            stm.setInt(1, Integer.parseInt(courseId));
            stm.setString(2, className);

//            正确代码
            String formattedTime = lectureTime.replace("T", " ") + ":00"; // 添加秒数
            stm.setTimestamp(3, Timestamp.valueOf(formattedTime));
            System.out.println("lectureTime:"+formattedTime);
            stm.setString(4, classroom);
            stm.setString(5, summary);
            int rows = stm.executeUpdate();

            if(rows > 0) {
                ObjectNode json = objectMapper.createObjectNode();
                json.put("success", true);
                out.print(json);
            } else {
                ObjectNode errorJson = objectMapper.createObjectNode();
                errorJson.put("error", "数据库操作失败");
                out.print(errorJson.toString());
            }


        }catch (SQLException e){
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
            out.print("{\"error\": \"" + e.getMessage() + "\"}");
        }

    }

}
